<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="850px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">客户登记单</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="90px">
        <el-tabs type="card" tab-position="top">
          <el-tab-pane label="基本信息">
            <el-row>
              <el-col :span="8">
                <el-form-item label="客户编号" prop="customerCode">
                  <el-input v-model.trim="ruleForm.customerCode" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="助记代码" prop="mnemonicCode">
                  <el-input v-model="ruleForm.mnemonicCode" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户名称" prop="customerName">
                  <el-input v-model="ruleForm.customerName" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="是否启用" prop="status">
                  <el-select v-model="ruleForm.status" clearable>
                    <el-option :value="1" label="启用"></el-option>
                    <el-option :value="0" label="停用"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户简称" prop="shortName">
                  <el-input v-model.trim="ruleForm.shortName" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="业务跟进" prop="businessFollowUp">
                  <el-input v-model.trim="ruleForm.businessFollowUp" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="跟进人电话" prop="tel">
                  <el-input v-model.trim="ruleForm.tel" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户联系人" prop="contactPerson">
                  <el-input v-model.trim="ruleForm.contactPerson" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系人手机" prop="phone">
                  <el-input v-model.trim="ruleForm.phone" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-form-item label="公司地址" prop="address">
                <el-input v-model="ruleForm.address" maxlength="50" show-word-limit />
              </el-form-item>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="单位电话" prop="telephone">
                  <el-input v-model.trim="ruleForm.telephone" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="单位传真" prop="fax">
                  <el-input v-model.trim="ruleForm.fax" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="公司网址" prop="website">
                  <el-input v-model.trim="ruleForm.website" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="总经理" prop="manager">
                  <el-input v-model.trim="ruleForm.manager" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="职务" prop="job">
                  <el-input v-model.trim="ruleForm.job" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="财务信息">
            <el-row>
              <el-col :span="12">
                <el-form-item label="开户银行" prop="bank">
                  <el-input v-model="ruleForm.bank" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="银行账号" prop="bankAccount">
                  <el-input v-model="ruleForm.bankAccount" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="单位折扣" prop="priceDiscount">
                  <el-input v-model="ruleForm.priceDiscount" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单价位数" prop="priceDigit">
                  <el-input v-model="ruleForm.priceDigit" maxlength="20" show-word-limit />
                </el-form-item>
              </el-col>
            </el-row>
            <el-col :span="24">
              <el-form-item label="开票地址" prop="billingAddress">
                <el-input v-model="ruleForm.billingAddress" type="textarea" rows="3" maxlength="500" show-word-limit />
              </el-form-item>
            </el-col>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-row>
    <!-- <span slot="footer">
      <el-button type="info" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </span>-->
  </el-dialog>
</template>
<script>
export default {
  data() {
    const checkCustomerCode = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/basic/customer/verifyCodeUnique?customerCode=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('客户编号重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      deptList: [],
      ruleForm: {
        bank: '',
        address: '',
        bankAccount: '',
        billingAddress: '',
        businessFollowUp: '',
        contactPerson: '',
        customerCode: '',
        customerName: '',
        fax: '',
        job: '',
        manager: '',
        mnemonicCode: '',
        phone: '',
        priceDigit: '',
        priceDiscount: '',
        shortName: '',
        status: '',
        tel: '',
        telephone: '',
        website: ''
      },
      treeProps: {
        value: 'id',
        label: 'deptName',
        children: 'children'
      },
      rules: {
        customerCode: [
          { required: true, message: '客户编号不能为空！', trigger: 'blur' },
          { validator: checkCustomerCode, trigger: 'blur' }
        ],
        customerName: [
          { required: true, message: '客户名称不能为空！', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      departData: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.dialogVisible = true
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
        console.log(row)
      } else {
        this.title = '新增'
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/basic/customer/save',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        bank: '',
        address: '',
        bankAccount: '',
        billingAddress: '',
        businessFollowUp: '',
        contactPerson: '',
        customerCode: '',
        customerName: '',
        fax: '',
        job: '',
        manager: '',
        mnemonicCode: '',
        phone: '',
        priceDigit: '',
        priceDiscount: '',
        shortName: '',
        status: '',
        tel: '',
        telephone: '',
        website: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
